﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using BLL.Common;
namespace App
{
    public partial class LoginApp : System.Web.UI.Page
    {
        private const string COOKIES_NAME = "SS_USERINFO"; //用户cookie名称
        private const char COOKIES_SPLITER = '-';       //cookie中的分隔符，用户名-密码。例如：张三-123
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //根据cookie取得语言
                if (Request.Cookies[COOKIES_NAME] != null)
                {
                    string userinfo = Request.Cookies[COOKIES_NAME].Value;
                    if (!string.IsNullOrEmpty(userinfo))
                    {
                        userinfo = Contexts.GetDecrypt(userinfo);
                        if (!string.IsNullOrEmpty(userinfo))
                        {
                            if (userinfo.IndexOf(COOKIES_SPLITER) > 0)
                            {
                                string username = userinfo.Split(new char[] { COOKIES_SPLITER })[0];
                                //password = userinfo.Split(new char[] { COOKIES_SPLITER })[1];

                                this.Txt_UserName.Value = username;
                                //this.Txt_Password.Value = password;
                                //this.Txt_Password.Attributes.Add();
                                rememberPassword.Checked = true;
                            }
                        }
                    }
                }
            }
        }

        protected void Btn_Login_Click(object sender, EventArgs e)
        {
            string userName = this.Txt_UserName.Value.ToString().Trim();
            string password = this.Txt_Password.Value.ToString().Trim();
            string validateCode = this.txtValidateCode.Value.ToString().Trim();


            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                lblErrorMessage.Visible = true;
                lblErrorMessage.Text = "用户名或密码不能为空。";

                return;
            }
            else
            {
                if (string.IsNullOrEmpty(validateCode))
                {
                    lblErrorMessage.Text = "验证码不能为空";
                    lblErrorMessage.Visible = true;
                    return;
                }
                else
                {
                    if (Session["ValidateCode"] == null)
                    {
                        lblErrorMessage.Text = "验证码已经过期，请刷新验证码后重试。";
                        lblErrorMessage.Visible = true;
                        return;
                    }

                    if (Session["ValidateCode"].ToString().ToLower() != validateCode.ToLower())
                    {
                        lblErrorMessage.Text = "验证码不正确。";
                        lblErrorMessage.Visible = true;
                        return;
                        Session["ValidateCode"] = null;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(this.Txt_UserName.Value) && !string.IsNullOrEmpty(this.Txt_Password.Value))
                        {
                            UserPassportInfo user = BLL.BUserPassport.getInstance().GetUserPassportInfo(this.Txt_UserName.Value, this.Txt_Password.Value);
                            
                            if (user != null && user.UserPassportId > 0)
                            {
                                user.ProfileInfo = BLL.BUserProfile.getInstance().GetUserProfileInfo(user.UserPassportId);
                                Session["LoginUserInfo"] = user;

                                //写入cookie
                                string rememberPwd = Contexts.GetStringFromRequest("rememberPassword");
                                if (rememberPwd.ToLower() == "on")
                                {
                                    //储存用户名，密码信息
                                    string cookieInfo = this.Txt_UserName.Value.ToString().Trim() + COOKIES_SPLITER.ToString() + this.Txt_Password.Value.ToString().Trim();

                                    HttpCookie cookie = new HttpCookie(COOKIES_NAME, Contexts.GetEncrypt(cookieInfo));
                                    cookie.Expires = DateTime.Now.AddDays(30);//30天后自动过期
                                    Response.Cookies.Add(cookie);//添加到客户端session则存在服务器。
                                }
                                else
                                {
                                    HttpCookie cookie = Request.Cookies[COOKIES_NAME];
                                    if (null != cookie)
                                    {
                                        cookie.Expires = DateTime.Now.AddMilliseconds(-1);
                                        Response.Cookies.Add(cookie);
                                    }
                                    //Request.Cookies.Remove(COOKIES_NAME);
                                }


                                string goUrl = Contexts.GetGoURL();
                                if (!string.IsNullOrEmpty(goUrl))
                                {
                                    Response.Redirect(goUrl);
                                }
                                else
                                {
                                    Response.Redirect(Contexts.AppPath + "/Index.aspx");
                                }
                            }
                            else
                            {
                                lblErrorMessage.Visible = true;
                                lblErrorMessage.Text = "用户名或密码错误。";

                                return;
                            }
                        }
                    }
                }
            }


        }
    }
}